package cc.wanforme.text2class.handler;

import cc.wanforme.text2class.entity.AnalysisConfig;
import cc.wanforme.text2class.entity.BeanField;
import cc.wanforme.text2class.entity.ClassConfig;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/** 生成 Java 类 */
public class SimpleClassCreator extends BaseClassCeator {

    private final String clazzTml = "{package}\n" +
            "\n" +
            "{imports}\n" +
            "{classComment}\n" +
            "{annotations}\n" +
            "public class {className} {\n" +
            "{classContent}\n" +
            "}";
    private final String classAttrContentTml =
            "    {comment}{annotations}\n" +
            "    private {type} {name}; {appendStr}\n";

    private static final DateTimeFormatter yyyyMMdd = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    /** BeanDef 转变成 class
     * @param config 基本信息
     * @param list 必填 beanDef
     * */
    @Override
    public String dataToClass(AnalysisConfig config, List<BeanField> list) {
        return generateClazz(config, list);
    }

    protected String generateClazz(AnalysisConfig config, List<BeanField> list) {
        String tmp = clazzTml;
        ClassConfig classConfig = config.getClassConfig();

        // 包名
        String packageName = classConfig.getPackageName();
        String packageRep = "";
        if (!BaseDataAnalysis.isBlank(packageName)) {
            packageRep = "package " + packageName + ";";
        }
        tmp = tmp.replace("{package}", packageRep);

        // 导包
        String imports = classConfig.getImports();
        String importsStr = BaseDataAnalysis.isBlank(imports) ? "" : imports;
        tmp = tmp.replace("{imports}", importsStr);

        // 注释
        String comment = classConfig.getComment();
        String commentStr = null;
        if (BaseDataAnalysis.isBlank(comment)) { // 没有注释，默认添加时间
            commentStr = "/** \n * " + yyyyMMdd.format(LocalDateTime.now()) + "\n */";
        } else {
            commentStr = comment;
        }
        tmp = tmp.replace("{classComment}", commentStr);

        // 注解
        String annotations = classConfig.getAnnotations();
        String annotationsStr = BaseDataAnalysis.isBlank(annotations) ? "" : ( "\n"+annotations );
        tmp = tmp.replace("{annotations}", annotationsStr);

        // 类名
        String name = classConfig.getName();
        String className = BaseDataAnalysis.isBlank(name) ? "" : name;
        tmp = tmp.replace("{className}", className);

        // 字段
        String fieldContent = this.handleBeanField(config, list);
        tmp = tmp.replace("{classContent}", fieldContent);

        return tmp;
    }

    /** 处理 类属性 */
    protected String handleBeanField(AnalysisConfig config, List<BeanField> list) {
        // 类具体属性内容
        StringBuilder sb = new StringBuilder();
        for (BeanField beanDef : list) {
            // 未能正确识别的
            if (!beanDef.isValid()) {
                sb.append("// unresolved! ").append(beanDef.getRawLine()).append("\n");
                continue;
            }

            // 数据类型
            String dataType = beanDef.getType();

            // 未处理的
            String unResolve = beanDef.getUnResolve();
            String appendStr = "";
            if (!BaseDataAnalysis.isBlank(unResolve)) {
                appendStr = "// " + unResolve;
            }

            // 注释
            String comment = "";
            String desc = beanDef.getDesc();
            if (!BaseDataAnalysis.isBlank(desc)) {
                comment = "/** " + desc + "*/";
//                comment += "\n@Column";
            }

            // 注解
            String annoStr = "";
            String annotations = beanDef.getAnnotations();
            if (!BaseDataAnalysis.isBlank(annotations)) {
                annoStr = "\n" + annotations;
            }

            String replace = classAttrContentTml
                    .replace("{comment}", comment)
                    .replace("{annotations}", annoStr)
                    .replace("{type}", dataType)
                    .replace("{name}", beanDef.getName())
                    .replace("{appendStr}", appendStr);
            sb.append(replace);
        }

        return sb.toString();
    }

}
